package stock;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Random;

import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.hibernate.Session;

import com.csvreader.CsvReader;

/**
 * 深圳的数据拉取
 * 
 * @author houzhuo
 *
 */
public class Test3 {

	/**
	 * 测试深圳下载EXCL
	 * 
	 * @param args
	 * @throws Exception
	 */
	public static void main(String[] args) throws Exception {

		HttpDownloadFile hdf = new HttpDownloadFile();
		for (int i = 0; i < 300; i++) {
		 
			// String date1 = DateTools.getDateTime(DateTools.getCurrentDateLong() - (i * 24
			// * 60 * 60 * 1000));
			// String date2 = DateTools.getDateTime(DateTools.getCurrentDateLong() - (i * 24
			// * 60 * 60 * 1000)).substring(0,10);
			// String date3 = DateTools.getDateTime(DateTools.getCurrentDateLong() - ((2) *
			// 24 * 60 * 60 * 1000));
			Date date0 = DateTools.getDateOffset(new Date(), -i); 
			String date1 = DateTools.getFormatDate(date0);
			String date4 = "2024-04-01";

			// System.out.println(date1);
			// System.out.println(date2);
			Random r = new Random();
			double d = r.nextDouble();
			String url = "http://www.szse.cn/api/report/ShowReport?SHOWTYPE=xlsx&CATALOGID=1815_stock_snapshot&TABKEY=tab1&txtBeginDate="
					+ date1 + "&txtEndDate=" + date1 + "&archiveDate=" + date4 + "&random=" + d;
			System.out.println(url);
			String downfile = "D:/stockdown/" + date1 + ".xlsx";
			hdf.down(url, downfile);
			Thread.sleep(2000);
			System.out.println("下载" + date1 + "成功");
			File f2 = new File(downfile);
			if (f2.exists()) {
				List<Stock> listStock = read(downfile);
				System.out.println("list" + listStock.size());
			}
		}

		// String date1 = DateTools.getCurrentDate();
		// String date2 = DateTools.getCurrentDate();
		// String date3 = DateTools.getDateTime(DateTools.getCurrentDateLong() - (2 * 24
		// * 60 * 60 * 1000));
		// String date4 = date3.substring(0, 10);

	}

	public static List<Stock> read(String filePath) throws FileNotFoundException, IOException {
		List<Stock> list = new ArrayList<Stock>();
		XSSFWorkbook xssfWorkbook = new XSSFWorkbook(new FileInputStream(filePath));
		System.out.println("xssfWorkbook对象：" + xssfWorkbook);
		// 读取第一个工作表
		XSSFSheet sheet = xssfWorkbook.getSheetAt(0);
		System.out.println("sheet对象：" + sheet);
		// 获取最后一行的num，即总行数。此处从0开始计数
		int maxRow = sheet.getLastRowNum();
		System.out.println("总行数为：" + (maxRow + 1));
		for (int row = 0; row <= maxRow; row++) {
			if (row == 0) {
				continue;
			}
			if (sheet.getRow(row).getCell(0).toString().length() <= 0) {
				break;
			}
			// System.out.print(sheet.getRow(row).getCell(cell) + " ");
			Stock stock = new Stock();
			for (int cell = 0; cell <= sheet.getRow(row).getLastCellNum() - 1; cell++) {
				if (cell == 0) {
					String date = sheet.getRow(row).getCell(0).toString();
					date = date.replace("-", "");
					stock.setDate(Integer.valueOf(date));
				}
				if (cell == 1) {
					stock.setCode(sheet.getRow(row).getCell(1).toString());
				}
				if (cell == 0) {
					stock.setName(sheet.getRow(row).getCell(2).toString());
				}
				if (cell == 0) {
					stock.setDanqianjia(Float.parseFloat(sheet.getRow(row).getCell(7).toString()));
				}
				if (cell == 0) {
					String str = sheet.getRow(row).getCell(9).toString();
					str = str.replace(",", "").replace(".", "");
					// System.out.println(str);
					if (str.length() > 0) {
						stock.setChengjiaoliang(Long.parseLong(str));
					} else {
						stock.setChengjiaoliang(0L);
					}

				}
				if (cell == 0) {
					stock.setZhandifu(Float.parseFloat(sheet.getRow(row).getCell(8).toString()));
				}
			}
			list.add(stock);
		}
		return list;
	}
}
